package DeportManage;

public class Name {
	private String firstName;//first name of customer
	private String middleName;//middle name of customer
	private String lastName;//last name of customer
	
	/**
	 * Constructor to create an object with full name of soccer player
	 * @param fullName  full name of customer
	 * @throws ErrorFormatNameException 
	 */
	public Name(String name) throws ErrorFormatNameException{
		String fullName = name.trim();
		int length = fullName.length();
		if((isSpace(fullName) >= 1 && isSpace(fullName) <= 2) && !fullName.substring(0,1).equals(" ") 
				&& !fullName.substring(length-1).equals(" ")){
			int spacepoint1 = fullName.indexOf(" ");// the index of 1st space point
			int spacepoint2 = fullName.lastIndexOf(" ");// the index of 2nd space point
			firstName = fullName.substring(0,spacepoint1);// get first name
			if(spacepoint1 == spacepoint2)
				middleName = "";
			else
				middleName = fullName.substring(spacepoint1+1,spacepoint2);// get middle name
			
			lastName =  fullName.substring(spacepoint2+1);// get last name
		}
		else 
			throw new ErrorFormatNameException(fullName);			
	}		
	/**
	 * Method to get the full name of customer
	 * @return full name of customer
	 */
	public String getFullName(){
		String fullname= firstName + " ";
		if(!middleName.equals(""))
			fullname += middleName + " ";
		fullname += lastName;
		return fullname;
	}	
	/**
	 * Method to check the string whether the number of space is not equal to 1 or 2.
	 * @param str the string to be checked whether the number of space is not equal to 1 or 2.
	 * @return int the number of space is not equal to 1 or 2.(true or false)
	 */
	private int isSpace(String string){
		int countspace = 0;
		for(int i=0; i< string.length();i++){
			char chr =string.charAt(i);
			if(chr == 32){
				countspace++;
				}
			}
	return countspace;
	}
}